package com.wbsxch.shiro.controller;

import com.wbsxch.shiro.dto.UserDTO;
import com.wbsxch.shiro.form.UserForm;
import com.wbsxch.shiro.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@RequiresPermissions({"user:manager","system:manager"})
public class UserController {

    @Autowired
    private UserService userService;

    @RequiresPermissions("add:user")
    @PostMapping("/")
    public String save(@RequestBody UserForm userForm){
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userForm,userDTO);
        userService.insert(userDTO);
        return "保存成功";
    }

    @RequiresPermissions("delete:user")
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id){
        userService.delete(id);
        return "删除成功";
    }

    @RequiresPermissions("update:user")
    @PutMapping("/{id}")
    public String update(@RequestBody UserForm userForm,@PathVariable Long id){
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userForm,userDTO);
        userDTO.setId(id);
        userService.update(userDTO);
        return "修改成功";
    }

    @RequiresPermissions("query:user")
    @GetMapping("/")
    public List<UserDTO> query(UserForm userForm){
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userForm,userDTO);
        List<UserDTO> userDTOS = userService.selectAll(userDTO);
        return userDTOS;
    }
}
